<?php

class CF_FullFilter extends CF_Filtering{

 /**
 * var $keyword_filter (default true) - adds '%' before and after on LIKE searches for keywords
 *
 */
	var $keyword_filter = true;

	/**
	 * get_filter_vals() will recieve the filter variables from Session or request
     * and set them in the filters array.
     * To clear filter, pass in a request with the filter set to nothing
     * ($variable1=&variable2=)
     * Filters are kept as filter [filter variable name][value]
     * Where filter variable name is filter prefix ($this->filterprefix) . variable name
	 */
	function get_filter_vals($method = 'req'){
		foreach($this->fieldspec AS $col_name => $colspec){
			if(is_array($colspec['filter'])){
				foreach($colspec['filter'] AS $opkey => $filterspec){
					$filtervar = $filterspec['filtervar'];
					$currentvalue = GetPersistent($filtervar,$method);
					print "Getting $filtervar = $currentvalue";	

					if(!is_null($currentvalue)){
					
						$this->filters[$filtervar]['var'] = $col_name;
						$this->filters[$filtervar]['val'] = $currentvalue;
						$this->filters[$filtervar]['op'] = $opkey;
					}else{
						unset($this->filters[$filtervar]['var']);
						unset($this->filters[$filtervar]['val']);
						unset($this->filters[$filtervar]['op']);
					}					
					
				}
			}
			// default, if filter is not an array, assume it is an equal filter
			else{
				$currentvalue = GetPersistent($this->filterprefix . $col_name,$method);
				if(!is_null($currentvalue)){
					$this->filters[$this->filterprefix . $col_name]['var'] = $col_name;
					$this->filters[$this->filterprefix . $col_name]['val'] = $currentvalue;
					$this->filters[$this->filterprefix . $col_name]['op'] = '=';
				}else{
					unset($this->filters[$this->filterprefix . $col_name]['var']);
					unset($this->filters[$this->filterprefix . $col_name]['val']);
					unset($this->filters[$this->filterprefix . $col_name]['op']);
				}
			}
        }
    }
	
	
	/**
	 * make_query_PSfilter will create a  prepare staement placeholder query filter according to fieldspec settings, Request input, and Session data
	 * 
	 */
	function make_query_PSfilter(){
		if(!empty( $this->filters)) {
            $tabledot = '';
			foreach($this->filters AS $filtername => $filter){
	// CT 2009-10-27 Added ' AND (!empty($this->fieldspec[$currentvar]['filter']))
	// CT 2009-11-07 Added this as new if stmt to the correct line a few lines below
                $currentvar = $filter['var'];
				if(!empty($this->fieldspec[$currentvar]['filter'])){
					if(isset($this->fieldspec[$currentvar]['source'])){
							$tabledot = $this->fieldspec[$currentvar]['source'] . '.';
						}
			
						
					$flt_text .= " AND $tabledot" . $filter['var'] . ' ' . $filter['op'] . " :FLT" . $filter['var'] . " ";
				}
			}	
		}
	
	return $flt_text;
	}
	/**
	 * make_query_PSfilter will create a  prepare staement placeholder query filter according to fieldspec settings, Request input, and Session data
	 * 
	 */
	function oldmake_query_PSfilter(){

		if(!empty( $this->filters)) {
            $tabledot = '';
			foreach($this->filters AS $filtername => $filter){
	// CT 2009-10-27 Added ' AND (!empty($this->fieldspec[$currentvar]['filter']))
	// CT 2009-11-07 Added this as new if stmt to the correct line a few lines below
                $currentvar = $filter['var'];
				if(!empty($this->fieldspec[$currentvar]['filter'])){
					if(isset($this->fieldspec[$currentvar]['source'])){
							$tabledot = $this->fieldspec[$currentvar]['source'] . '.';
						}
					$flt_text .= " AND $tabledot" . $filter['var'] . " = :FLT" . $filter['var'] . " ";
				}
			}	
		}
	
	return $flt_text;
	}
	
	function make_PSfilter_bindings(&$stmt){
			if(!empty( $this->filters)) {
            $tabledot = '';
			foreach($this->filters AS $filtername => $filter){
                $currentvar = $filter['var'];
				if(!empty($this->fieldspec[$currentvar]['filter'])){
					if(isset($this->fieldspec[$currentvar]['source'])){
							$tabledot = $this->fieldspec[$currentvar]['source'] . '.';
						}
					
						$bindvar = ':FLT'. $filter['var'];
						$value = $filter['val'];
						if($filter['op'] == 'LIKE' AND $this->keyword_filter == true){
							$value = '%' . $value . '%';
							}							

						$stmt->bindValue($bindvar,$value);
				}
			}	
		}
	}


	
	
}?>